﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using WHelper = PGMHelper.WilsonHelper;
using PGMHelper;

using PGMHelper.ProgressForm;

namespace HystereticBasic
{
    /// <summary>
    /// 数据列表
    /// </summary>
    public class HyDataSet
    {
        /// <summary>
        /// 数据列表
        /// </summary>
        public List<BasicData> DataList { set; get; }

        /// <summary>
        /// 获得所有数据
        /// </summary>
        public List<GridViewDatas> GetGridDataList()
        {
            return (from data in this.DataList
                    select data.GetData()).ToList();
        }

        /// <summary>
        /// 设定延性系数
        /// </summary>
        /// <param name="isPos"></param>
        public DuctilityObjSet GetDuctilityObjSet(bool isPos, float UltimateStrengthCo, float ParkCo)
        {
            return new DuctilityObjSet(this, isPos, UltimateStrengthCo, ParkCo);
        }

        /// <summary>
        /// 生成XLSX
        /// </summary>
        public void ToXLSX(bool isPosNegSeperate, bool isFilter)
        {
            //要导出的参数
            var dataTypeList = new List<DataType>
            {
                DataType.AbsCycleMaxDisp, 
                DataType.AccumulateEnergy, DataType.ViscousDampingRatio,
                DataType.SecantStiffness, DataType.SecantStiffnessRatio,
                DataType.AbsUnloadDisp, DataType.UnloadDispRatio,
                DataType.AbsCycleMaxForce, DataType.CycleMaxForceRatio,
            };
            //初始化储存列表
            var dataList = new List<GridViewDatas>();
            //遍历参数
            dataTypeList.ForEach(dataType =>
            {
                //初始化对象
                var data = new GridViewDatas(HystereticHelper.GetDescp(dataType));
                //遍历构件
                this.DataList.ForEach(basicData =>
                {
                    data.AddRange(basicData.GetData(new List<DataType>
                    { DataType.CycleIndex, dataType }, isPosNegSeperate, isFilter));
                });
                dataList.Add(data);
            });
            //导出数据
            NPOI.Helper.NPOIExcelHelper.SaveDatasToExcel(dataList, true, "构件参数合辑");
        }

        /// <summary>
        /// 设定基础数据
        /// </summary>
        /// <param name="obj"></param>
        private void SetBasicData(object obj)
        {
            //获得进程
            var p = (Process)obj;
            //获得参数
            var param = (Tuple<List<string>, int, SkeletonSoftenMethod>)p.Param;
            //设定最大值
            p.SetMainProcessBoundary(param.Item1.Count);
            //数据数目
            var length = param.Item1.Count();
            //遍历
            foreach (var path in param.Item1)
            {
                //文件名称
                var name = Path.GetFileNameWithoutExtension(path);
                //修改名称
                p.SetMessage(name);
                //构造数据
                var data = new BasicData(path, param.Item2, param.Item3);
                //计算颜色
                data.PosDataColor = GetColor(this.DataList.Count, length * 2);
                data.NegDataColor = GetColor(this.DataList.Count + length, length * 2);
                //添加数据
                this.DataList.Add(data);
                //进度地政
                p.AddMainProcess(1);
            }
            p.CloseFrom(true);
        }

        #region Construction

        /// <summary>
        /// 根据列表长度和元素索引生成颜色
        /// </summary>
        /// <param name="i"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        private Color GetColor(int i, int length)
        {
            //如果长度为1，返回黑色
            if (length == 1) return Color.Black;

            int m = (int)(Math.Pow(length, 1.0 / 3)) + 1;
            int s = 255 / (m - 1);
            //R值
            int c1 = i % m;
            int d1 = c1 / 2;
            int R = c1 % 2 == 0 ? d1 * s : 255 - d1 * s;
            //G值
            int c2 = i % (m * m) / m;
            int d2 = c2 / 2;
            int G = c2 % 2 == 0 ? d2 * s : 255 - d2 * s;
            //B值
            int c3 = i / (m * m);
            int d3 = c3 / 2;
            int B = c3 % 2 == 0 ? d3 * s : 255 - d3 * s;

            return Color.FromArgb(R, G, B);
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="directPath"></param>
        public HyDataSet(List<string> filePathList, SkeletonSoftenMethod method)
            :this(filePathList, 0, method)
        {
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="directPath"></param>
        public HyDataSet(List<string> filePathList, int skipLine, SkeletonSoftenMethod method)
              : this()
        {
            //构造参数
            var paramList = Tuple.Create<List<string>, int, SkeletonSoftenMethod>(filePathList, skipLine, method);
            //进程
            Process.Run(this.SetBasicData, ProgressType.Simple, paramList);
        }

        /// <summary>
        /// 初始话
        /// </summary>
        public HyDataSet()
        {
            this.DataList = new List<BasicData>();
        }

        #endregion
    }
}
